package com.cqust.icc.task5.service.imp;

import com.cqust.icc.task5.domain.Account;
import com.cqust.icc.task5.service.AccountService;
import com.cqust.icc.task5.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/4/7 15:14
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT, readOnly = false,rollbackFor = Exception.class)
    public void Transfer(Account out, Account in, String str) {
        in = getById(in.getAccountName());

        float outnum = Float.parseFloat(getById(out.getAccountName()).getAccountBalance());
        float num = Float.parseFloat(str);

        in.setAccountBalance(String.valueOf(Float.parseFloat(in.getAccountBalance()) +num));
        updateAccount(in);
        if (outnum<num){
            throw new RuntimeException("余额不足");
        }

//        Update(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));

    }

    public void addAccount(Account account) {
        System.out.println("addAccount");
        System.out.println(account);

        // 调用方法实行
        SqlSession session = MyBatisUtils.getSession();
        int updat = session.update("AccountMapper.insertAccount",account);
        // 返回成功的条数
//        System.out.println(updat);

    }

    public List<Account> selectAllAccountList() {
        SqlSession session = MyBatisUtils.getSession();
//        String sql = "select * from zcc.account";
        return session.selectList("AccountMapper.selectAccountList",null);

    }

    public void updateAccount(Account account) {
        SqlSession session = MyBatisUtils.getSession();
        System.out.println("updateAccount");
        System.out.println(account);
        String sql = "UPDATE   zcc.account SET     account_balance = ? WHERE account_name = ?";
        Object[] args = {account.getAccountBalance(),account.getAccountName()};
        int updat = session.update("AccountMapper.updateAccount",account);
//        System.out.println(updat);

    }

    public void deleteAccountById(int id) {
        SqlSession session = MyBatisUtils.getSession();
        int updat = session.delete("AccountMapper.deleteAccountById",id);
//        System.out.println(updat);


    }

    public void withdrawMoney(Account account) {
        SqlSession session = MyBatisUtils.getSession();
        System.out.println("withdrawMoney");
        System.out.println(account);
        int updat = session.update("AccountMapper.withdrawMoney",account);
        System.out.println(updat+"ccc");
    }

    public void Update(String str) {
        SqlSession session = MyBatisUtils.getSession();
        String sql = "UPDATE tb_log set method_name = 'update by  " + str + "' WHERE zcc.tb_log.id =7";
        int update = session.update("AccountMapper.withdrawMoney");
        if (update > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }

    public Account getById(String str) {
        SqlSession session = MyBatisUtils.getSession();

        return session.selectOne("AccountMapper.selectAccountByName", str);


    }

}
